#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
 umask(0);
 // int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    int fd = open("log.txt", O_CREAT | O_WRONLY | O_APPEND, 0666);//从当前光标位置追加
    int fd1=open("x.txt",O_CREAT | O_RDWR | O_TRUNC,0666);
    const char *p="我是x.txt文件\n";
    write(fd1,p,strlen(p));
   if(fd < 0)
   {
       perror("open");
       return 1;
   }
   lseek(fd1, 0, SEEK_SET);
// printf("%d",fd);

  char buffer[128];
  int r=dup2(fd1,0);
  //printf("%d",r);
  fgets(buffer,sizeof(buffer),stdin);
  //printf("%s",buffer);
   write(fd,buffer,strlen(buffer));

   close(3);

}
